﻿@using CodeGenerator.Core
@using System.Text
@{
    int count = 0;
    string primaryKey = "";
    TableInfo table = Model.TableInfo;
    string mName = CodeGenerator.Common.Utils.Pascal(Model.Name);    
}
using NHibernate;
using NHibernate.Transform;
using Shengyi.BaseDao.Helper;
using Shengyi.Common;
using Shengyi.Haoyouju;
using Shengyi.Kefu_ViewModel.Domain.SYWeb;
using Shengyi.ViewModel.Domain;
using Shengyi.Model.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Shengyi.Kefu_Dao.Repository
{
    public class @(mName)Dao : BaseDao.Mysql.BaseRepository
    {

        ///<summary>
        /// 获取列表
        ///</summary>
        public IList<@(mName)Model> Get@(mName)List(HtmlViewCondition htmlView, @(mName)ListSearchVM search, out int totalSize)
        {
            const string sqlColumn ="a.*";
            const string sql ="SELECT {0} FROM @table.Name a WHERE 1=1 {1} {2} ";
            string orderStr = GetOrderBySql(htmlView.SortField, htmlView.SortOrder);
            #region 条件
            string strWhere = "";
            @foreach (ColumnInfo column in table.ColumnList)
            {
                if (column.IsPrimaryKey == "否")
                {
                    if (column.NetType.Alias == "string")
                    {
            @:if(search.@(column.Name).IsNotNull())
                @:strWhere += " AND @(column.Name) LIKE '%{0}%'".AddFormat(search.@(column.Name).ToSafeSqlString());
                    }
                    else if (column.NetType.Alias == "Guid")
                    {
            @:if(search.@(column.Name).IsNotNullOrNotAll())
                @:strWhere += " AND @(column.Name) = '{0}'".AddFormat(search.@(column.Name).ToSafeSqlString());
                    }
                    else
                    {
            @:if(search.@(column.Name).IsNotNullOrNotAll())
                @:strWhere += " AND @(column.Name) = '{0}'".AddFormat(search.@(column.Name).ToSafeSqlString());
                    }
                }
            }
            #endregion 条件
            string querySql = sql.AddSqlFormat(sqlColumn, strWhere, orderStr);
            string countSql = sqlCount.AddSqlFormat(querySql);

            var key = BuildQueryUniqueKey(querySql+htmlView.ToString());
            var result = MemcacheHelper.GetValue<IList<@(mName)Model>>(key);
            if (result.IsNull())
            {
                result = Session.CreateSQLQuery(querySql)
                    .SetResultTransformer(Transformers.AliasToBean<@(mName)Model>())
                    .SetFirstResult((int)((htmlView.CurrentPage - 1) * htmlView.PageSize))
                     .SetMaxResults((int)(htmlView.PageSize))
                    .List<@(mName)Model>();
                MemcacheHelper.UpdateValue(key, result, DateTime.Now.AddSeconds(30));
            }

            var keyCount = BuildQueryUniqueKey(countSql);
            var count = MemcacheHelper.GetValue<object>(keyCount);
            if (count == null)
            {
                count = Session.CreateSQLQuery(countSql).UniqueResult();
                MemcacheHelper.UpdateValue(keyCount, count, DateTime.Now.AddSeconds(30));
            }
            totalSize = Convert.ToInt32(count);
            return result;
        }    

        /// <summary>
        /// 获取列表
        /// </summary>
        public IList<@(mName)Model> Get@(mName)List(Expression<Func<@(mName)Model, bool>> where)
        {
            var rtn = Session.QueryOver<@(mName)Model>().Where(where).OrderBy(d => d.UpdateTime).Desc.List();
            return rtn;
        }

        /// <summary>
        /// 获取列表ByIds
        /// </summary>
        public IList<@(mName)Model> Get@(mName)List(IList<Guid> ids, Expression<Func<@(mName)Model, bool>> where)
        {
            if (where.IsNull())
            {
                var rtn = Session.QueryOver<@(mName)Model>().WhereRestrictionOn(d => d.Id).IsIn(ids.ToArray()).List();
                return rtn;
            }
            else
            {
                var rtn = Session.QueryOver<@(mName)Model>().WhereRestrictionOn(d => d.Id).IsIn(ids.ToArray()).And(where).List();
                return rtn;
            }            
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        public @(mName)Model Get@(mName)Model(Expression<Func<@(mName)Model, bool>> where)
        {
            var rtn = Session.QueryOver<@(mName)Model>().Where(where).SingleOrDefault();
            return rtn;
        }
    }
}